﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication9
{
    public partial class Formlogin : Form
    {
        DataTable dt = null;
        public Formlogin()
        {
            InitializeComponent();
        }

        private void Formlogin_Load(object sender, EventArgs e)
        {
            try
            {
                dt = Class1.getLoginTable();//获取管理员表
                this.txtPsw.MaxLength = 6;
                txtPsw.PasswordChar = '*';
                //初始化Combox
                this.comboBox1.DataSource = dt;
                this.comboBox1.DisplayMember = "Username";
                this.comboBox1.ValueMember = "Username";
                if (this.comboBox1.Items.Count > 0)
                    this.comboBox1.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.Items.Count <= 0)
                {
                    MessageBox.Show("用户名称不能空!", "提示",
                            MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                string account = this.comboBox1.SelectedValue.ToString();
                string pwd = this.txtPsw.Text;
                string filter = "Username='" + account + " 'and Password='" + pwd + "'";
                if (string.IsNullOrEmpty(pwd))
                {
                    filter = "Username='" + account + " 'and (Password is null or Password='')";
                }
                DataRow[] rows = dt.Select(filter);
                if (rows.Length <= 0)
                {
                    DialogResult dr = MessageBox.Show("密码错误", " 提示",
                        MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
                    if (dr == DialogResult.Retry)
                    {
                        txtPsw.SelectAll();
                        txtPsw.Focus();
                        return;
                    }
                    else
                    {
                        Application.Exit();
                        return;
                    }


                }
                Program.name = this.comboBox1.Text;
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            this.Dispose();
        }



       
    }
}
